package com.aso114.project.mvp.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BaseSimpleActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ASUS on 2019/1/29.
 * banner  web界面
 */

public class BannerDetaiActivity extends BaseSimpleActivity {
    @BindView(R.id.layout_title_bar_back_iv)
    ImageView layoutTitleBarBackIv;
    @BindView(R.id.layout_title_bar_title_tv)
    TextView layoutTitleBarTitleTv;
    @BindView(R.id.wv)
    WebView wv;
    @BindView(R.id.myProgress)
    ProgressBar bar;

    @Override
    protected int getResouseId() {
        return R.layout.bannerdetail;
    }

    @SuppressLint("JavascriptInterface")
    @Override
    protected void initView() {


        layoutTitleBarTitleTv.setText("内容详情");
        String linkUrl = getIntent().getStringExtra("linkUrl");


        wv.setLongClickable(true);

        // 设置WebView属性，能够执行Javascript脚本
        wv.getSettings().setJavaScriptEnabled(true);
        // 加载新闻具体内容网页

        // 自适应屏幕
        WebSettings settings = wv.getSettings();
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        // 支持javascript
        wv.getSettings().setJavaScriptEnabled(true);

        wv.addJavascriptInterface(new InJavaScriptLocalObj(), "local_obj");
        // 商品详情
        // http://192.168.3.112/fobaoWX/buddhawx/Goods/index/goods_id/304.html
        // http://www.cnblogs.com/hibraincol/
        // http://192.168.1.104/fobaoWX/buddhawx/shop/index/shop_id/"+shopid+"/mobile/4.html

        wv.loadUrl(linkUrl);// http://www.fobao.com/m-Mobile
        wv.setWebViewClient(new MyWebViewClient());

        // 加载网页时显示进度条
        wv.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    bar.setVisibility(View.INVISIBLE);
                } else {
                    if (View.INVISIBLE == bar.getVisibility()) {
                        bar.setVisibility(View.VISIBLE);
                    }
                    bar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

        });
    }

    @Override
    // 设置回退
    // 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
            wv.goBack(); // goBack()表示返回WebView的上一页面
            return false;
        }

        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.layout_title_bar_back_iv, R.id.layout_title_bar_title_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_title_bar_back_iv:
                finish();
                break;
            case R.id.layout_title_bar_title_tv:
                break;
        }
    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {// 网页页面开始加载的时候
            super.onPageStarted(view, url, favicon);
        }

        // 加载网页结束时 获取网页内容
        public void onPageFinished(WebView view, String url) {
            Log.d("WebView", "onPageFinished ");

            view.loadUrl("javascript:window.local_obj.showSource('<head>'+"
                    + "document.getElementsByTagName('html')[0].innerHTML+'</head>');");

            String title = view.getTitle();
            if (!TextUtils.isEmpty(title)) {
//                titleTv.setText(title);
            }

            CookieManager cookieManager = CookieManager.getInstance();

            String CookieStr = cookieManager.getCookie(url);

            Log.e("sunzn", "Cookies = " + CookieStr);

            System.out.println("获取网页中的Cookies==" + CookieStr);
            super.onPageFinished(view, url);
        }

        // 监听 所有点击的链接，如果拦截到我们需要的，就跳转到相对应的页面。
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) { // 网页加载时的连接的网址
            view.loadUrl(url);
            System.out
                    .println("----------------------shouldOverrideUrlLoading 。。 url:"
                            + url);
            return false;
        }
    }

    final class InJavaScriptLocalObj {
        public void showSource(String html) {
            Log.d("HTML", html);
            System.out.println("js返回结果" + html);// 处理返回的结果
        }
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void getBroadcast(Context context, Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
